package com.yiwei.ydd.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.api.model.LeveListModel;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.constant.Const;
import com.yiwei.ydd.constant.UserInfoGlobal;
import com.yiwei.ydd.event.BuyVipUpSuccessEvent;
import com.yiwei.ydd.util.Arith;
import com.yiwei.ydd.util.Spans;
import com.yiwei.ydd.util.UI;
import com.yiwei.ydd.view.PagesDetailDialog;
import com.yiwei.ydd.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class VipPacketSecondActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.img_icon)
    ImageView imgIcon;
    @BindView(R.id.txt_status)
    TextView txtStatus;
    @BindView(R.id.txt_tips_one)
    TextView txtTipsOne;
    @BindView(R.id.txt_tips_two)
    TextView txtTipsTwo;
    @BindView(R.id.img_icon_sec)
    ImageView imgIconSec;
    @BindView(R.id.txt_info_sec)
    TextView txtInfoSec;
    @BindView(R.id.txt_status_sec)
    TextView txtStatusSec;
    @BindView(R.id.txt_tips_one_sec)
    TextView txtTipsOneSec;
    @BindView(R.id.txt_tips_two_sec)
    TextView txtTipsTwoSec;
    @BindView(R.id.img_icon_three)
    ImageView imgIconThree;
    @BindView(R.id.txt_info)
    TextView txtInfo;
    @BindView(R.id.txt_status_three)
    TextView txtStatusThree;
    @BindView(R.id.txt_tips_one_three)
    TextView txtTipsOneThree;
    @BindView(R.id.txt_tips_two_three)
    TextView txtTipsTwoThree;
    @BindView(R.id.txt_info_three)
    TextView txtInfoThree;
    @BindView(R.id.layout_packet_one)
    RelativeLayout layoutPacketOne;
    @BindView(R.id.layout_packet_two)
    RelativeLayout layoutPacketTwo;
    @BindView(R.id.layout_packet_three)
    RelativeLayout layoutPacketThree;
    private ArrayList<LeveListModel.DatasBean> levelList;
    private PagesDetailDialog pagesDetailDialog;
    private Unbinder unbiner;
    private int x24;
    private int x30;
    private int x36;
    private int x18;
    private int selectBuy = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip_packet_second);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
        EventBus.getDefault().register(this);
    }

    private void init() {
        pagesDetailDialog = new PagesDetailDialog(this).setKeyword(Const.PageDetailType.LEVELCOMBO);
        levelList = getIntent().getParcelableArrayListExtra("data");
        txtTitle.setText("会员套餐");
        txtRight.setText("价格说明");
        txtRight.setVisibility(View.VISIBLE);

        if (UserInfoGlobal.getUserLevel() == 1) {
            layoutPacketOne.setVisibility(View.GONE);
        } else if (UserInfoGlobal.getUserLevel() == 2) {
            layoutPacketOne.setVisibility(View.GONE);
            layoutPacketTwo.setVisibility(View.GONE);
        } else if (UserInfoGlobal.getUserLevel() == 3) {
            layoutPacketOne.setVisibility(View.GONE);
            layoutPacketTwo.setVisibility(View.GONE);
            layoutPacketThree.setVisibility(View.GONE);
        }


        if (levelList != null && levelList.size() != 0) {

            x18 = getResources().getDimensionPixelSize(R.dimen.x18);
            x24 = getResources().getDimensionPixelSize(R.dimen.x24);
            x30 = getResources().getDimensionPixelSize(R.dimen.x30);
            x36 = getResources().getDimensionPixelSize(R.dimen.x36);
            for (int i = 0; i < levelList.size(); i++) {
                LeveListModel.DatasBean item = levelList.get(i);
                switch (i) {
                    case 0:
                        txtTipsOne.setText("说明：成功注册即可成为银卡会员，无需缴纳费用");
                        Spanned textOne = Spans.with(this).font("￥", x30, 0xffffffff).
                                font(item.money, x36, 0xffFB5E01).
                                font("元", x18, 0xffffffff).
                                font("\n银卡会员", x24, 0xffFEFEFE).
                                build();
                        txtInfo.setText(textOne);
                        UI.setTvHtml(txtTipsTwo, R.string.vip_packet_sec, Float.valueOf(item.sale) / 10 + "", item.term_sale + "%", item.uolevel_rate + "%", item.utlevel_rate + "%");
                        break;
                    case 1:
                        txtTipsOneSec.setText("说明：需支付" + item.money + "元的升级会员费用");
                        Spanned textTwo = Spans.with(this).
                                font("￥"+item.money, x36, 0xffFABF1F).
                                font("\n"+levelList.get(1).name, x24, 0xff5E524F).
                                build();
                        txtInfoSec.setText(textTwo);
                        UI.setTvHtml(txtTipsTwoSec, R.string.vip_packet_sec, Float.valueOf(item.sale) / 10 + "", item.term_sale + "%", item.uolevel_rate + "%", item.utlevel_rate + "%");

                        break;
                    case 2:
                        String paymoney = "";
                        if (UserInfoGlobal.getUserLevel() == 2) {
                            paymoney = Arith.sub(Double.valueOf(levelList.get(2).money), Double.valueOf(levelList.get(1).money)) + "";
                        } else {
                            paymoney = levelList.get(2).money;
                        }
                        txtTipsOneThree.setText("说明：需支付" + paymoney + "元的升级会员费用");
                        Spanned textThree = Spans.with(this).
                                font("￥" + paymoney, x36, 0xff8300FF).
                                font("\n" + levelList.get(2).name, x24, 0xff5E524F).
                                build();
                        txtInfoThree.setText(textThree);
                        UI.setTvHtml(txtTipsTwoThree, R.string.vip_packet_sec, Float.valueOf(item.sale) / 10 + "", item.term_sale + "%", item.uolevel_rate + "%", item.utlevel_rate + "%");

                        break;
                }
            }
        }
        if (UserInfoGlobal.getUserLevel() == 1) {
            txtStatus.setText("当前等级");
            txtStatusSec.setText("立即购买");
            txtStatusThree.setText("立即购买");
        } else if (UserInfoGlobal.getUserLevel() == 2) {
            txtStatus.setText("已获等级");
            txtStatusSec.setText("当前等级");
            txtStatusThree.setText("立即购买");
        } else if (UserInfoGlobal.getUserLevel() == 3) {
            txtStatus.setText("已获等级");
            txtStatusSec.setText("已获等级");
            txtStatusThree.setText("当前等级");
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(BuyVipUpSuccessEvent event) {
        finish();
    }


    public void goToBuyLevel() {
        if (selectBuy == 1) {
            if (levelList != null && levelList.size() >= 2) {
                Intent intent = new Intent(this, VipOrderPayMoneyActivity.class);
                intent.putExtra("level_id", levelList.get(1).id + "");
                intent.putExtra("pay_money", levelList.get(1).money);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
            }
        } else if (selectBuy == 2) {
            if (levelList != null && levelList.size() >= 3) {
                Intent intent = new Intent(this, VipOrderPayMoneyActivity.class);
                intent.putExtra("level_id", levelList.get(2).id + "");

                String paymoney = "";
                if (UserInfoGlobal.getUserLevel() == 2) {
                    paymoney = Arith.sub(Double.valueOf(levelList.get(2).money), Double.valueOf(levelList.get(1).money)) + "";
                } else {
                    paymoney = levelList.get(2).money;
                }
                intent.putExtra("pay_money", paymoney);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.txt_status, R.id.txt_status_sec, R.id.txt_status_three, R.id.btn_back, R.id.txt_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_status:
                break;
            case R.id.txt_status_sec:
                selectBuy = 1;
                if (UI.toString(txtStatusSec).equals("立即购买")) {
                    goToBuyLevel();
                }
                break;
            case R.id.txt_status_three:
                selectBuy = 2;
                if (UI.toString(txtStatusThree).equals("立即购买")) {
                    goToBuyLevel();
                }
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:

                pagesDetailDialog.show();
//                Dialog.showVipPacketTips(this);
                break;
        }
    }

}
